const App = getApp()

import {
    url
} from '../../modelApi/url_config.js'
Page({
    data: {
        hasNextPage: true,
        placeholder: "",
        showNavBar: true,
        shopcartCount: 0,
        bannerList: [],
        winHeight: "", //高度
        liveIndex: '0', //tab切换
        dayShopParams: {
            "isNew": 1,
            "pageNo": 1,
            "pageSize": 10
        }, //今日团品列表参数
        dayShopList: [], //今日团品列表
    },
    bindscroll: function (a) {
        a.detail.scrollTop > 50 ? this.setData({
            showNavBar: false
        }) : this.setData({
            showNavBar: true
        });
    },
    onLoad() {
        this.loadBanner();
        this.loadDayShop();
        //  高度自适应
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                that.setData({
                    winHeight: calc
                });
            }
        });
    },
    onShow() {
        wx.hideTabBar();
        this.geCartListFun();
        this.getMenu();
    },
    loadBanner() {
        // 获取banner
        App.ajax(url.getBanner, {}, 'get').then(res => {
            this.setData({
                bannerList: res.data
            })
        })
    },
    /* 切换tab */
    switchNav: function (t) {
        if (t.target.dataset.liveIndex === this.data.liveIndex) return
        var e = t.target.dataset.liveIndex;
        this.data.dayShopParams.pageNo = 0
        this.setData({
            liveIndex: e,
            hasNextPage: true,
            dayShopList: [],
            dayShopParams: this.data.dayShopParams
        });
        this.loadMore()
    },
    /* 获取今日团品 */
    loadDayShop() {
        App.ajax(url.goodsListUrl, this.data.dayShopParams, 'post').then(res => {
            this.data.dayShopList = [...this.data.dayShopList, ...res.rows]
            this.setData({
                dayShopList: this.data.dayShopList,
                hasNextPage: res.hasNextPage
            })
        })
    },
    /* 获取为你推荐 */
    loadRecShop() {
        App.ajax(url.goodsListUrl, this.data.dayShopParams, 'post').then(res => {
            this.data.dayShopList = [...this.data.dayShopList, ...res.rows]
            this.setData({
                dayShopList: this.data.dayShopList,
                hasNextPage: res.hasNextPage
            })
        })
    },
    loadMore() {
        if (this.data.hasNextPage) {
            this.data.dayShopParams.pageNo++
        } else {
            return;
        }
        if (this.data.liveIndex == 0) {
            this.loadDayShop();
        } else {
            this.loadRecShop();
        }
    },
    /* 获取购物车列表 */
    geCartListFun() {
        App.ajax(url.cartList, {}, 'get').then(res => {
            this.setData({
                shopcartCount: res.data.length
            })
        })
    },
    getMenu() {
        App.ajax(url.menuList, {versionNo:'1'}, 'get').then(res => {
            if (res.data.isShow) {
                wx.showTabBar();
            }
        })
    }
});